

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Core.Serialization;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Store.Services.CouponService;
using JTC.MiniCommerce.Store.Web.Controls;
using JTC.MiniCommerce.Localization;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin.controls.configuration.couponproviders {
  public partial class percentoffconfiguration : CouponConfigurationControl {
  
    #region Member Variables
    
    private int couponId = 0;
    
    #endregion

    #region Page Events
        
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetPercentOffConfigurationProperties();
        couponId = Utility.GetIntParameter("couponId");
        if(couponId > 0) {
          Coupon coupon = new Coupon(couponId);
          Serializer serializer = new Serializer();
          PercentOffCouponProvider percentOffCouponProvider = serializer.DeserializeObject(coupon.ValueX, coupon.Type) as PercentOffCouponProvider;
          lblCouponId.Text = coupon.CouponId.ToString();
          txtCouponCode.Text = coupon.CouponCode;
          txtExpirationDate.Text = coupon.ExpirationDate.ToString();
          txtPercentOff.Text = percentOffCouponProvider.PercentOff.ToString();
          chkIsSingleUse.Checked = coupon.IsSingleUse;
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(percentoffconfiguration).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }     
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      try {
        int couponId = 0;
        int.TryParse(lblCouponId.Text, out couponId);
        Coupon coupon;
        if(couponId == 0) {
          coupon = new Coupon();
        }
        else {
          coupon = new Coupon(couponId);
        }
        
        PercentOffCouponProvider percentOffCouponProvider = new PercentOffCouponProvider();
        decimal percentOff = 0M;
        decimal.TryParse(txtPercentOff.Text, out percentOff);
        percentOffCouponProvider.PercentOff = percentOff;
        
        coupon.CouponCode = txtCouponCode.Text;
        DateTime expirationDate = DateTime.UtcNow;
        DateTime.TryParse(txtExpirationDate.Text, out expirationDate);
        coupon.ExpirationDate = expirationDate;
        coupon.IsSingleUse = chkIsSingleUse.Checked;
        coupon.Type = percentOffCouponProvider.GetType().AssemblyQualifiedName;
        coupon.ValueX = new Serializer().SerializeObject(percentOffCouponProvider, typeof(PercentOffCouponProvider));
        coupon.Save(WebUtility.GetUserName());
        Response.Redirect("~/admin/coupons.aspx", true);
      }
      catch(Exception ex) {
        Logger.Error(typeof(percentoffconfiguration).Name + ".btnSave_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnGenerate control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnGenerate_Click(object sender, EventArgs e) {
      try {
        txtCouponCode.Text = CoreUtility.GenerateRandomString(8);
      }
      catch(Exception ex) {
        Logger.Error(typeof(percentoffconfiguration).Name + ".btnGenerate_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }     
    }
    
    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Sets the percent off control configuration properties.
    /// </summary>
    private void SetPercentOffConfigurationProperties() {
      ltDescription.Text = HttpUtility.HtmlDecode(base.Provider.Description);
      LocalizationUtility.AddLabelText(lblCouponCode);
      LocalizationUtility.AddHoverHelp(hlCouponCodeHelp);
      LocalizationUtility.AddLabelText(lblExpirationDate);
      LocalizationUtility.AddHoverHelp(hlExpirationDateHelp);
      LocalizationUtility.AddLabelText(lblPercentOff);
      LocalizationUtility.AddHoverHelp(hlPercentOffHelp);
      LocalizationUtility.AddLabelText(lblIsSingleUse);
      LocalizationUtility.AddHoverHelp(hlIsSingleUseHelp);
      LocalizationUtility.AddButtonText(btnSave);
      lblDescriptionTitle.Text = LocalizationUtility.GetText("lblPercentOffCouponDescriptionTitle");
      LocalizationUtility.AddButtonText(btnGenerate);
    }    
    
    #endregion
    
    #endregion
    
  }
}